﻿using System;
using System.Html;
using enyo;
using onyx;

namespace JenkinsView
{
    public class App : enyo.FittableRows, IObserver
    {
        [EvalAtCompile, NoCompile]
        private const string V_SVR_LIST = "serverList";

        [EvalAtCompile, NoCompile]
        private const string V_JOB_LIST = "jobList";

        [EvalAtCompile, NoCompile]
        private const string V_DETAIL_LIST = "jobDetailList";

        [EvalAtCompile, NoCompile]
        private const string EDT_SVR = "editSvr";

        [EvalAtCompile, NoCompile]
        private const string PNL = "mainPanel";

        [EvalAtCompile, NoCompile]
        private const string DLG_WAIT = "waitDlg";

        [EvalAtCompile, NoCompile]
        private const string DLG_CONFIRM = "confirmDlg";

        [EvalAtCompile, NoCompile]
        private const string DLG_ERROR = "errDlg";

        protected new string Classes = "onyx";
        private new object[] Components = new Control[] {
            new Panels() { Kind = enyo.ComponentKind.Panels, Name = PNL, Fit = true, Classes = "panels-sliding", ArrangerKind = ArrangerKind.CollapsingArranger, Wrap = false,
                Components = new Component[]{
                    new Control(){
                        Kind = "JenkinsView.ServerListView", Name = V_SVR_LIST
                    },

                    new Control(){
                        Kind = "JenkinsView.JobListView", Name = V_JOB_LIST
                    },

                    new Control(){
                        Kind = "JenkinsView.JobDetailListView", Name = V_DETAIL_LIST
                    }
                }
            },            

            new ServerEditDialog() { Kind = "JenkinsView.ServerEditDialog", Name = EDT_SVR, Centered = true, Modal = true, ScrimWhenModal = true, AutoDismiss = false },

            new WaitDialog(){ Kind = "JenkinsView.WaitDialog", Name = DLG_WAIT, Modal = true, Floating = true, Centered = true, ScrimWhenModal = true, AutoDismiss = false },
            
            new ErrorDialog() { Kind = "JenkinsView.ErrorDialog", Name = DLG_ERROR, Modal = true, Floating = true, Centered = true, ScrimWhenModal = true, AutoDismiss = false },

            new ErrorDialog() { Kind = "JenkinsView.ConfirmDialog", Name = DLG_CONFIRM, Modal = true, Floating = true, Centered = true, ScrimWhenModal = true, AutoDismiss = false }
        };

        public override void Create()
        {
            this.Inherited(Arguments.Value);

            //Note:Hook up view/model
            var svrList = this._[V_SVR_LIST] as ServerListView;
            var jobList = this._[V_JOB_LIST] as JobListView;
            var jobDetailList = this._[V_DETAIL_LIST] as JobDetailListView;

            DomainModel.Model.AddView(this);
            DomainModel.Model.AddView(svrList);
            DomainModel.Model.AddView(jobList);
            DomainModel.Model.AddView(jobDetailList);

            //Note:Start up with local saved data
            DomainModel.Model.ServerManager.LoadServersFromStorage();
        }

        private C FindControl<C>(string name) where C : class
        {
            return this._[name] as C;
        }

        public void Update(int reason, object arg)
        {
            switch (reason)
            {
                case DomainModel.PANEL_NAVIGATE:
                    var pnl = this.FindControl<Panels>(PNL);
                    int direction = (int)arg;
                    if (direction == DomainModel.NEXT_VIEW)
                        pnl.Next();
                    if (direction == DomainModel.PREV_VIEW)
                        pnl.Previous();
                    break;

                case DomainModel.ASYNC_CALL:
                    var dlg = this.FindControl<WaitDialog>(DLG_WAIT);
                    int act = (int)arg;
                    if (act == DomainModel.WAIT)
                        dlg.Show();
                    if (act == DomainModel.DONE)
                        dlg.Hide();
                    break;

                case DomainModel.EDIT_SVR:
                    var editor = this.FindControl<ServerEditDialog>(EDT_SVR);
                    editor.Clear();
                    editor.Show();
                    break;

                case DomainModel.CONFIRM_DEL:
                    var confirmDlg = this.FindControl<ConfirmDialog>(DLG_CONFIRM);
                    confirmDlg.Show();
                    break;

                case DomainModel.ERROR:
                    var err = this.FindControl<ErrorDialog>(DLG_ERROR);
                    err.Show();
                    break;
            }
        }
    }
}
